//
//  MyWalletViewController.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 20/04/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "MyWalletViewController.h"

@interface MyWalletViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * walletTableView;
}
@end

@implementation MyWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView * bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
    
    bgView.frame = CGRectMake(0, 0, bgView.image.size.width, bgView.image.size.height);
    [self.view addSubview:bgView];
    
    __weak MyWalletViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"钱包",@"title",@"账户明细",@"rightTitle", nil] leftAction:^(UIButton * sender){
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:^(UIButton * sender){
        UIViewController * controller = [[CTMediator sharedInstance] walletDetailController];
        [weakSelf.navigationController pushViewController:controller animated:YES];
    }];
    navView.backgroundColor = [UIColor clearColor];
    walletTableView = [self addTableViewInView:self.view frame:CGRectMake(0, navView.frame.size.height + navView.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - (navView.frame.size.height + navView.frame.origin.y)) delegateAndDataSource:self];
    walletTableView.backgroundColor = UIColorFromRGB(0xE9E9E9);
    walletTableView.scrollEnabled = NO;
    
    [[CTMediator sharedInstance] getMyWalletData:^(NSDictionary * dic){
        
    }];
}

-(void)addSegButtons:(ElementTableViewCell *)cell{
    CGRect frame = CGRectMake(0, 0, walletTableView.frame.size.width / 2, cell.cellHeight);
    for (NSInteger i = 0 ; i < 2; i ++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        [cell addSubview:button];
        
        UILabel * valueLabel = [[UILabel alloc] init];
        
        valueLabel.frame = CGRectMake(0, 20, button.frame.size.width, (button.frame.size.height - 40) / 2);
        
        valueLabel.font = [UIFont systemFontOfSize:14];
        valueLabel.textAlignment = NSTextAlignmentCenter;
        valueLabel.textColor = UIColorFromRGB(0x20C67A);
        [button addSubview:valueLabel];
        
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0, valueLabel.frame.size.height + valueLabel.frame.origin.y, button.frame.size.width, valueLabel.frame.size.height);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
        
        if (i == 0) {
            label.text = @"累计收入(元)";
        }else{
            label.text = @"待结算收益(元)";
        }
        [button addSubview:label];
#if kDevelopment
        if (i == 0) {
            valueLabel.text = @"2861.90";
        }else{
            valueLabel.text = @"2861.90";
        }
#endif
        frame.origin.x += frame.size.width;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if([indexPath section] == 0){
        if([indexPath row] == 0){
            cell.backgroundColor = [UIColor clearColor];
            
            UILabel * valueLabel = [[UILabel alloc] init];
            valueLabel.frame = CGRectMake(0, 30, tableView.frame.size.width, 40);
            valueLabel.text = @"399.90";
            valueLabel.textAlignment = NSTextAlignmentCenter;
            valueLabel.font = [UIFont systemFontOfSize:30];
            valueLabel.textColor = [UIColor whiteColor];
            [cell addSubview:valueLabel];
            
            UILabel * unitLabel = [[UILabel alloc] init];
            unitLabel.textAlignment = NSTextAlignmentCenter;
            unitLabel.frame = CGRectMake(0, valueLabel.frame.origin.y + valueLabel.frame.size.height + 10, tableView.frame.size.width, 20);
            unitLabel.text = @"余额(元)";
            unitLabel.font = [UIFont systemFontOfSize:14];
            unitLabel.textColor = [UIColor whiteColor];
            [cell addSubview:unitLabel];
            cell.cellHeight = unitLabel.frame.size.height + unitLabel.frame.origin.y + 30;
            
            
            UIImageView * bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
            
            bgView.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.cellHeight);
            [cell addSubview:bgView];
            [cell sendSubviewToBack:bgView];
        }else if([indexPath row] == 1){
            cell.cellHeight = 100;
            
            [self addSegButtons:cell];
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(tableView.frame.size.width / 2, 10, 0.5,cell.cellHeight - 20)];
            lineView.backgroundColor = UIColorFromRGB(0xE9E9E9);
            [cell addSubview:lineView];
        }
        
    }else if([indexPath section] == 1){
        cell.cellHeight = 50;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = UIColorFromRGB(0x333333);
        if([indexPath row] == 0){
            cell.textLabel.text = @"押金";
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, cell.cellHeight - 1.0, tableView.frame.size.width, 0.5)];
            lineView.backgroundColor = UIColorFromRGB(0xE9E9E9);
            [cell addSubview:lineView];
        }else{
            cell.textLabel.text = @"提现";
        }
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
        return v;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 20;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

@end
